package com.dofast.bt.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dofast.bt.R;
import com.dofast.bt.base.BaseApplication;
import com.dofast.bt.bean.EnjoyBean;
import com.dofast.bt.bean.GoodsBean;

import org.xutils.ex.DbException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * username:Created by hsj on 2016/12/22 0022.
 * class：
 * 类描述：收藏的宝贝
 */

public class CollectGoodsListAdapter extends BaseAdapter {
    private List<EnjoyBean> list = new ArrayList<EnjoyBean>();
    private SelectCallBack callBack;

    public CollectGoodsListAdapter(List<EnjoyBean> list, SelectCallBack callBack) {
        this.list = list;
        this.callBack = callBack;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (null == convertView) {
            convertView = LayoutInflater.from(BaseApplication.getInstance().getApplicationContext()).inflate(R.layout.item_collect_goods, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        EnjoyBean enjoyBean = list.get(position);
        try {
            GoodsBean goodsBean = enjoyBean.getGoods();

        holder.llDelete.setVisibility(View.GONE);
        //店铺名称
        holder.tvName.setText(goodsBean.getName());
        holder.tvPrice.setText("¥" + goodsBean.getPrice() + "");
        //店铺头像

        Glide.with(BaseApplication.getInstance().getApplicationContext()).load(goodsBean.getImg_head()).placeholder(R.mipmap.tea).into(holder.ivPhoto);
        holder.ivPhoto.setBackgroundResource(R.drawable.shap_o_image);

        if (enjoyBean.isCheck()) {
            holder.llDelete.setVisibility(View.VISIBLE);
        }
        holder.llDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.select(position, true);
            }
        });
        } catch (DbException e) {
            e.printStackTrace();
        }
        return convertView;
    }


    static class ViewHolder {
        @BindView(R.id.iv_photo)
        ImageView ivPhoto;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.ll_delete)
        LinearLayout llDelete;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
